package com.c.ctgproject.ui.service

import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.service.PayTypeEntity
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.ServicePayTypeViewModel
import com.nanchen.compresshelper.CompressHelper
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.activity_change_pay_way.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/7/12
 *描述:修改支付方式
 */
class ChangePayWayActivity : BaseActivity() {
    private val mPayViewModel by lazy { ViewModelProvider(this)[ServiceOrderDetailViewModel::class.java] }
    private val mViewModel by lazy { ViewModelProvider(this)[ServicePayTypeViewModel::class.java] }
    override fun getLayout() = R.layout.activity_change_pay_way
    private val suborderId by lazy { intent.getStringExtra(MyConstants.SUBORDER_ID).handlerNull() }
    private var weChatFile: File? = null
    private var payType = -1
    override fun event() {
        mPayViewModel.payType(suborderId)
        mPayViewModel.payTypeResult.observe(this, {
            MyLog.d("收款方式回调:$it")
            handlerHttpResult(it) {
                val data = getGson().fromJson(it, PayTypeEntity::class.java)
                if (data.data==null){
                    mAliPayBTN.performClick()
                }
                data?.data?.let { bean ->
                    removeAllView()
                    when (bean.paymentMethod) {
                        "1" -> {
                            //支付宝
//                            mAliPayBTN.backgroundResource = R.drawable.bg_btn_green_radius_5
                            mAliPayBTN.performClick()
                            mAliPayNumberET.textSet(bean.aliPayAccount.handlerNull())
                            mAliPayNameET.textSet(bean.aliPayName.handlerNull())
                        }
                        "2" -> {
                            //微信
//                            mWeChatBTN.backgroundResource = R.drawable.bg_btn_green_radius_5
                            mWeChatBTN.performClick()
                            Glide.with(this).load(bean.code.handlerNull()).into(mWeChatIV)
                        }
                        "3" -> {
                            //银行卡
//                            mBankBTN.backgroundResource = R.drawable.bg_btn_green_radius_5
                            mBankBTN.performClick()
                            mBankNumberET.textSet(bean.bankAccount.handlerNull())
                            mBankUserNameET.textSet(bean.cardholder.handlerNull())
                            mBankNameET.textSet(bean.openingBank.handlerNull())
                            mBankDetailNameET.textSet(bean.subBranch.handlerNull())
                        }
                        "4" -> {
                            //现金
                            mMoneyBTN.performClick()
                        }
                    }
                }
            }
        })

        mViewModel.result.observe(this, {
            MyLog.d("提交收款方式回调:$it")
            handlerHttpResult(it) {
                loading.dismiss()
                showToast("提交收款方式成功")
                finish()
            }
        })
        mViewModel.error.observe(this, {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })
        mPayViewModel.error.observe(this, {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })


        mRadioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.mAliPayBTN -> {
                    removeAllView()
                    mAliPayLL.visibility = View.VISIBLE
                    payType = 1
                }
                R.id.mWeChatBTN -> {
                    removeAllView()
                    mWeChatLL.visibility = View.VISIBLE
                    payType = 2
                }
                R.id.mBankBTN -> {
                    removeAllView()
                    mBankLL.visibility = View.VISIBLE
                    payType = 3
                }
                R.id.mMoneyBTN -> {
                    removeAllView()
                    payType = 4
                }
            }
        }
    }

    private fun removeAllView() {
        mAliPayLL.visibility = View.GONE
        mWeChatLL.visibility = View.GONE
        mBankLL.visibility = View.GONE
    }

    override fun click() {
        //添加微信收款码
        mWeChatIV.setOnClickListener {
            ImageUtil.initSingleImagePicker(this, 1234)
        }
        //提交
        mConfirmBTN.setOnClickListener {
            when (payType) {
                1 -> {
                    val aliNumber = mAliPayNumberET.text.handlerNull()
                    val aliName = mAliPayNameET.text.handlerNull()
                    when {
                        aliNumber.isEmpty() -> showToast("请输入支付宝账号")
                        aliName.isEmpty() -> showToast("请输入支付宝名称")
                        else -> {
                            loading.show()
                            mViewModel.aliPayChat(
                                handlerBody(
                                    "suborderId" to suborderId,
                                    "aliPayAccount" to aliNumber,
                                    "aliPayName" to aliName,
                                )
                            )
                        }
                    }

                }
                2 -> {
                    if (weChatFile != null) {
                        loading.show()
                        val array = ArrayList<MultipartBody.Part>()
                        val file= CompressHelper.getDefault(this).compressToFile(weChatFile!!)
                        val requestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), file)
                        array.add(MultipartBody.Part.createFormData("wxPayCode", weChatFile!!.name, requestBodyImage))
                        val requestBodyText = MultipartBody.Part.createFormData("suborderId", suborderId)
                        array.add(requestBodyText)
                        mViewModel.payWeChat(array)
                    } else {
                        showToast("请选择微信收款码")
                    }
                }
                3 -> {
                    val bankNumber = mBankNumberET.text.handlerNull()
                    val bankDetail = mBankDetailNameET.text.handlerNull()
                    val bankUserName = mBankUserNameET.text.handlerNull()
                    val bankName = mBankNameET.text.handlerNull()
                    when {
                        bankNumber.isEmpty() -> showToast("请输入银行卡号")
                        bankDetail.isEmpty() -> showToast("请输入支行名称")
                        bankUserName.isEmpty() -> showToast("请输入持卡人姓名")
                        bankName.isEmpty() -> showToast("请输入银行名称")


                        else -> {
                            loading.show()
                            mViewModel.bank(
                                handlerBody(
                                    "suborderId" to suborderId,
                                    "bankAccount" to bankNumber,
                                    "subBranch" to bankDetail,
                                    "cardholder" to bankUserName,
                                    "openingBank" to bankName,
                                )
                            )
                        }
                    }
                }
                4 -> {
                    //现金
                    loading.show()
                    mViewModel.payMoney(suborderId)
                }
                else -> {
                    showToast("请选择一种支付方式")
                }
            }
        }


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1234 && data != null) {
            //图片路径 同样视频地址也是这个 根据requestCode
            val mSelected = Matisse.obtainResult(data)
            MyLog.d("图片地址:$mSelected")
            if (mSelected.isNotEmpty()) {
                weChatFile = ImageUtil.changeFile(this, mSelected[0])
                weChatFile?.let {
                    Glide.with(this).load(weChatFile).into(mWeChatIV)
                }
            }
        }
    }
}